Ecosystème

Column

Présentation

Bienvenue sur le tableau de bord associé à mon mémoire de maîtrise !

Ce document numérique a pour but de faciliter la lecture des résultats issus de mon mémoire de maîtrise.

Pour rappel, mon sujet de recherche s’intitule :

Agriculture 4.0 et Grappes Industrielles d’Innovation : Une Analyse de Science des Données Géospatiales

Chaque onglet de ce tableau de bord a pour but de présenter un aspect de la méthodologie de recherche, les figures sont interactives et présentent des informations complémentaires lorsque l’on clique sur les différents éléments. Les définitions nécessaires à comprendre la totalité des éléments du tableau de bord sont présentées dans le chapitre 3 de méthodologie du mémoire.

Les onglets présentent les informations suivantes :

  • “Écosystème” présente le positionnement géographique des entités identifiées comme appartenant à l’écosystème d’innovation numérique agricole
  • “Exemples sectoriels” présente les différentes variables représentés et calculés pour des secteurs donnés.
  • “Cartographie des secteurs” présente de façon globale les variations de distance et de positionnements en fonction des secteurs agricoles
  • “Analyse des variables” présente comment les variables d’âge et du nombre d’employés impactent les distances moyennes au signal de l’écosystème et les écart-types associés
  • “Repenser l’agriculture : Clusters agricoles” présente les résultats d’un partitionnement des exploitations agricoles pour minimiser les distances entre les exploitations ainsi que la composition sectorielle des clusters obtenus

Le code ayant donné lieu à ces résultats est visible dans l’onglet “Source code” et a été codé en R sur la plateforme Rstudio mise à ma disposition par Nuance_R.

Bonne lecture !

LRI

Column

Cartographie du positionnement des entités de la grappe industrielle d’innovation numérique et influences associées

Céréales

Column

Cartographie du positionnement du centre des sous-secteurs d’activité céréalière

Column

Distance du centre des sous-secteurs avec le signal de l’écosystème et nombre d’exploitations associées

Cartographie de la distance moyenne des entreprises des sous-secteurs à l’écosystème et écart-type associé

Cartographie des secteurs

Column

Cartographie du centre des secteurs agricoles et du signal de l’écosystème d’innovation

Column

Distance du centre des secteurs avec le signal de l’écosystème et nombre d’exploitations associées

Cartographie de la distance moyenne des entreprises des sous-secteurs à l’écosystème et écart-type associé

Impact du nombre d’employé

Column {data-width = “330”}

Graphique des distances moyennes des entreprises des secteur à l’écosystème en fonction de l’écart-type

Column

Graphique des distances moyennes en fonction du nombre d’employé

Graphique des ecart-types en fonction du nombre d’employé

Impact de l’âge

Rows {data-width = 660}

Graphique des distances moyennes en fonction de l’âge des exploitations

Rows {data-width = 660}

Graphique des écart-types en fonction de l’âge des exploitations

Repenser l’agriculture : Clusters agricoles

Column

Nombre optimal de clusters WSS

Visualisation de la composition des clusters

Column

Visualisation des entreprises

Visualisation des clusters

---
title: "Innovation Numérique Agricole au Québec"
author: "LRI"
output: 
  flexdashboard::flex_dashboard:
    theme : readable
    logo: CIRANO.jpeg
    source_code: embed
---

```{r setup, include=FALSE, echo=FALSE, message = FALSE, warning = FALSE}
knitr::opts_chunk$set(echo = TRUE)

#    runtime: shiny
library(dplyr)
library(readxl)
library(readr)
library(ggplot2)
library(ggthemes)
library(DT)
library(flexdashboard)
library(tidyverse)
library(lubridate)
library(tidyr)
library(plotly)
library("stringr")
library("plyr")
library("readr")
library(Req)
library(leaflet)
library(dplyr)
library(factoextra)
library(geojsonsf)
library(shiny)
library(RColorBrewer)
library(mapview)
library(webshot)

library(leafem)

## On importe les données concernant l'écosystème
ecosysteme <- gsheet::gsheet2tbl("https://docs.google.com/spreadsheets/d/1XwSgrbq5P6CKFkroaciYULswxxGA0aPig1Yojcrt6tM/edit?usp=sharing")
ecosysteme$Moyenne <- as.numeric(ecosysteme$Moyenne)




```

```{r, echo=FALSE, message = FALSE, warning = FALSE,include = FALSE}
## On créé la matrice de distance avec le centroïde de l'écosystème
distance <- data.frame(matrix(ncol=3, nrow=0))
colnames(distance) <- c("Categorie","Longitude","Latitude")

distance[1,] <- c(ecosysteme$Nom_ent[2],ecosysteme$Longitude[2],ecosysteme$Latitude[2])
distance$Longitude <- round(as.numeric(distance$Longitude),5)
distance$Latitude <- round(as.numeric(distance$Latitude),5)
distance[1,4] <- "Écosystème" 
distance[1,5:11] <- 1
colnames(distance)[4] <- "Secteur"
colnames(distance)[5] <- "Complet" ## Catégorie prenant tous les exploitations du secteur agricole
colnames(distance)[6] <- "emp_sup_10" ## Catégorie des exploitations de plus de 10 employés
colnames(distance)[7] <- "emp_inf_10" ## Catégorie des exploitations de moins de 10 employés
colnames(distance)[8] <- "age_inf_5" ## Catégorie des entreprises agricoles de moins de 5 ans
colnames(distance)[9] <- "age_inf_15_sup_5" ## Catégorie des entreprises agricoles de plus de 5 ans et moins de 15 ans
colnames(distance)[10] <- "age_sup_15" ## Catégorie des entreprises agricoles de plus de 15 ans
colnames(distance)[11] <- "Dist_centre" ## Distance avec le centroïde de la grappe industrielle
```

# Ecosystème

## Column {data-width="300"}

### Présentation

<style>
body {
text-align: justify}
</style>

**Bienvenue sur le tableau de bord associé à mon mémoire de maîtrise !**

Ce document numérique a pour but de faciliter la lecture des résultats issus de mon mémoire de maîtrise. 

Pour rappel, mon sujet de recherche s'intitule : 

*Agriculture 4.0 et Grappes Industrielles d'Innovation : Une Analyse de Science des Données Géospatiales*

Chaque onglet de ce tableau de bord a pour but de présenter un aspect de la méthodologie de recherche, les figures sont interactives et présentent des informations complémentaires lorsque l'on clique sur les différents éléments. Les définitions nécessaires à comprendre la totalité des éléments du tableau de bord sont présentées dans le chapitre 3 de méthodologie du mémoire.


Les onglets présentent les informations suivantes :

- "Écosystème" présente le positionnement géographique des entités identifiées comme appartenant à l'écosystème d'innovation numérique agricole
- "Exemples sectoriels" présente les différentes variables représentés et calculés pour des secteurs donnés.
- "Cartographie des secteurs" présente de façon globale les variations de distance et de positionnements en fonction des secteurs agricoles
- "Analyse des variables" présente comment les variables d'âge et du nombre d'employés impactent les distances moyennes au signal de l'écosystème et les écart-types associés
- "Repenser l'agriculture : Clusters agricoles" présente les résultats d'un partitionnement des exploitations agricoles pour minimiser les distances entre les exploitations ainsi que la composition sectorielle des clusters obtenus

Le code ayant donné lieu à ces résultats est visible dans l'onglet "Source code" et a été codé en R sur la plateforme Rstudio mise à ma disposition par Nuance_R.

Bonne lecture !

LRI

### {data-height="75"}

![](./polytechnique_gauche_rgb_2.jpeg)

## Column {data-width="700"}

### Cartographie du positionnement des entités de la grappe industrielle d'innovation numérique et influences associées

```{r, echo=FALSE, message = FALSE, warning = FALSE}

ecosysteme <- ecosysteme[-1,]

ecosysteme$Moyenne2 <- round(ecosysteme$Moyenne^2,2)

## On cartographie l'écosystème
leaflet(data = ecosysteme) %>% 
  setView(-72.8, 46.1, 8) %>%
  addTiles() %>% 
  addCircleMarkers(lng = ~Longitude, lat = ~Latitude, ## On ajoute les points de coordonnées des entreprises
      radius = ~Moyenne2, stroke = FALSE, fillOpacity = 0.8, 
      color= ~colorBin('OrRd',Moyenne)(Moyenne), 
      popup = ~ paste("Nom:", Nom_ent, "<br/>","Signal :", Moyenne,"<br/>","Impact des services :", Impact)) %>%
  addCircleMarkers(lng = ecosysteme$Longitude[1], lat = ecosysteme$Latitude[1], ## On ajoute le centroïde de l'écosystème
      radius = 25, stroke = FALSE, fillOpacity = 0.9, 
      color = "steelblue",
      popup = paste("Nom:", ecosysteme$Nom_ent[1], "<br/>","Signal :", ecosysteme$Moyenne[1],"<br/>","Impact des services :", ecosysteme$Impact[1]) )  %>%
  addLegend(
    "bottomright",
    title = "Signal de l'entreprise",
    pal = colorBin('OrRd', ecosysteme$Moyenne),
    values = ecosysteme$Impact, 
    opacity = 0.9) 
    
  
```

# Céréales {data-navmenu="Exemples sectoriels"}

## Column {data-width="570"}

### Cartographie du positionnement du centre des sous-secteurs d'activité céréalière

```{r, echo=FALSE, message=FALSE,warning=FALSE}
df_0131 <- Req::Req_data(industry = 0131, active = 1) %>%
  filter(province == "Quebec") ## On récupères les données de la REQ par API
```

```{r, echo=FALSE, message = FALSE, warning = FALSE}

distance[2,1] <- "Céréales"
distance[2,2] <- round(mean(df_0131$Long,na.rm = TRUE),5)
distance[2,3] <- round(mean(df_0131$Lat,na.rm = TRUE),5) ## On ajoute les coordonnées des centroïdes du secteur d'activité dans la matrice de distance
distance[2,4] <- "Céréales"
distance[2,5] <- 1
distance[2,12] <- length(df_0131$Long) ## Calcul de nombre d'entreprises agricoles appartenant à la catégorie
distance[2,13] <- round(mean(sqrt((df_0131$Lat - mean(df_0131$Lat,na.rm = TRUE))^2 + (df_0131$Long - mean(df_0131$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[2,14] <- round(sd(sqrt((df_0131$Lat - mean(df_0131$Lat,na.rm = TRUE))^2 + (df_0131$Long - mean(df_0131$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[2,15] <- round(sd(sqrt((df_0131$Lat - ecosysteme$Latitude[1])^2 + (df_0131$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[2,16] <- round(mean(sqrt((df_0131$Long - ecosysteme$Longitude)^2 + (df_0131$Lat - ecosysteme$Latitude)^2)),4)


index <- df_0131 %>%
  filter(COD_INTVAL_EMPLO_QUE != "N" & COD_INTVAL_EMPLO_QUE != "O"
         & COD_INTVAL_EMPLO_QUE != "A" 
         & COD_INTVAL_EMPLO_QUE != "B" )
distance[3,1] <- "Céréales & emp > 10" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 10 employés
distance[3,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[3,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[3,4] <- "Céréales"
distance[3,6] <- 1
distance[3,12] <- length(index$Long)
distance[3,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[3,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[3,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[3,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0131 %>%
  filter(COD_INTVAL_EMPLO_QUE != "E" & COD_INTVAL_EMPLO_QUE != "D" & COD_INTVAL_EMPLO_QUE != "C")
distance[4,1] <- "Céréales & emp < 10" ## On ajoute la catégorie des entreprises agricoles céréalières de moins de 10 employés
distance[4,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[4,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[4,4] <- "Céréales"
distance[4,7] <- 1
distance[4,12] <- length(index$Long)
distance[4,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[4,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[4,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[4,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0131 %>%
  filter(DAT_STAT_IMMAT > "2018-01-01")
distance[5,1] <- "Céréales & age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de moins de 5 ans
distance[5,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[5,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[5,4] <- "Céréales"
distance[5,8] <- 1
distance[5,12] <- length(index$Long)
distance[5,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[5,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[5,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[5,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0131 %>%
  filter(DAT_STAT_IMMAT < "2018-01-01" & DAT_STAT_IMMAT > "2008-01-01" )
distance[6,1] <- "Céréales & 15 < age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans et moins de 15 ans
distance[6,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[6,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[6,4] <- "Céréales"
distance[6,9] <- 1
distance[6,12] <- length(index$Long)
distance[6,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[6,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[6,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[6,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0131 %>%
  filter(DAT_STAT_IMMAT < "2008-01-01")
distance[7,1] <- "Céréales & age > 15" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 15 ans
distance[7,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[7,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[7,4] <- "Céréales"
distance[7,10] <- 1
distance[7,12] <- length(index$Long)
distance[7,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[7,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[7,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[7,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

```

```{r, echo=FALSE, message = FALSE, warning = FALSE}
leaflet(data = distance[-1,]) %>%
  addTiles() %>% 
  addCircleMarkers(lng = ecosysteme$Longitude[1], lat = ecosysteme$Latitude[1], ## On ajoute le centroïde de l'écosystème
      radius = 25, stroke = FALSE, fillOpacity = 0.9, 
      color = "steelblue", popup = ecosysteme$Nom_ent[1]) %>%
  addAwesomeMarkers(popup = ~Categorie)
```

## Column {data-width="430"}

### Distance du centre des sous-secteurs avec le signal de l'écosystème et nombre d'exploitations associées

```{r, echo=FALSE, message = FALSE, warning = FALSE}
Long_centre <-distance$Longitude[1]
Lat_centre <- distance$Latitude[1]
distance[,11] <- round(sqrt((distance$Longitude - Long_centre)^2 + (distance$Latitude - Lat_centre)^2),4)
colnames(distance)[11] <- "Dist_centre" ## On calcule la distance entre le centre de la catégorie et l'écosystème
colnames(distance)[12] <- "nb_ent" ## On compte le nombre d'entreprise remplissant les critères définis
colnames(distance)[13] <- "moyenne_dist" ## On calcule la distance moyenne entre les entreprises d'un secteur et son centre
colnames(distance)[14] <- "ecart_type_dist" ## On calcule l'écart type des distance entre les entreprises d'une catégorie donnée
colnames(distance)[15] <- "ecart_type_eco" ## On calcule l'écart type des distances entre les entreprises d'une catégorie et le centre de l'écosystème
colnames(distance)[16] <- "dist_moy_centre" ## On calcule la distance moyenne entre les entreprises d'un secteur et le centre de l'écosystème

```

```{r, echo=FALSE, message = FALSE, warning = FALSE}
datatable(distance[-1,c(1,11,12)],
            rownames = FALSE, 
              options = list(pageLength = 5, scrollX = TRUE,dom = 'tip'), 
              class = 'white-space: nowrap' )
```



### Cartographie de la distance moyenne des entreprises des sous-secteurs à l'écosystème et écart-type associé

```{r, echo=FALSE, message=FALSE,warning=FALSE}
p <- distance[] %>%
  filter(Secteur=="Céréales") %>%
    ggplot() +
    geom_point(aes(dist_moy_centre,ecart_type_eco,color=Categorie)) +
    ylab("Ecart-type") +
    xlab("Distance moyenne") +
    theme_minimal() 
      
  ggplotly(p)%>% #Ploty du graph
  config(displayModeBar = FALSE)
  
```





```{r Culture du maïs (sauf le maïs fourrager et le maïs sucré) (0134) , echo=FALSE, message=FALSE,warning=FALSE}
df_0134 <- Req::Req_data(industry = 0134, active = 1) %>%
  filter(province == "Quebec") ## On récupères les données de la REQ par API
```


```{r Culture du maïs (sauf le maïs fourrager et le maïs sucré) (0134) 2, echo=FALSE, message = FALSE, warning = FALSE}

distance[8,1] <- "Maïs"
distance[8,2] <- round(mean(df_0134$Long,na.rm = TRUE),5)
distance[8,3] <- round(mean(df_0134$Lat,na.rm = TRUE),5) ## On ajoute les coordonnées des centroïdes du secteur d'activité dans la matrice de distance
distance[8,5] <- 1
distance[8,12] <- length(df_0134$Long)
distance[8,13] <- round(mean(sqrt((df_0134$Lat - mean(df_0134$Lat,na.rm = TRUE))^2 + (df_0134$Long - mean(df_0134$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[8,14] <- round(sd(sqrt((df_0134$Lat - mean(df_0134$Lat,na.rm = TRUE))^2 + (df_0134$Long - mean(df_0134$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[8,15] <- round(sd(sqrt((df_0134$Lat - ecosysteme$Latitude[1])^2 + (df_0134$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[8,16] <- round(mean(sqrt((df_0134$Long - ecosysteme$Longitude)^2 + (df_0134$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0134 %>%
  filter(COD_INTVAL_EMPLO_QUE != "N" & COD_INTVAL_EMPLO_QUE != "O"
         & COD_INTVAL_EMPLO_QUE != "A" 
         & COD_INTVAL_EMPLO_QUE != "B" )
distance[9,1] <- "Maïs & emp > 10" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 10 employés
distance[9,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[9,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[9,6] <- 1
distance[9,12] <- length(index$Long)
distance[9,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[9,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[9,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[9,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0134 %>%
  filter(COD_INTVAL_EMPLO_QUE != "E" & COD_INTVAL_EMPLO_QUE != "D" & COD_INTVAL_EMPLO_QUE != "C")
distance[10,1] <- "Maïs & emp < 10" ## On ajoute la catégorie des entreprises agricoles céréalières de moins de 10 employés
distance[10,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[10,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[10,7] <- 1
distance[10,12] <- length(index$Long)
distance[10,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[10,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[10,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[10,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0134 %>%
  filter(DAT_STAT_IMMAT > "2018-01-01")
distance[11,1] <- "Maïs & age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans
distance[11,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[11,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[11,8] <- 1
distance[11,12] <- length(index$Long)
distance[11,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[11,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[11,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[11,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0134 %>%
  filter(DAT_STAT_IMMAT < "2018-01-01" & DAT_STAT_IMMAT > "2008-01-01" )
distance[12,1] <- "Maïs & 15 < age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans et moins de 15 ans
distance[12,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[12,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[12,9] <- 1
distance[12,12] <- length(index$Long)
distance[12,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[12,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[12,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[12,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0134 %>%
  filter(DAT_STAT_IMMAT < "2008-01-01")
distance[13,1] <- "Maïs & age > 15" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 15 ans
distance[13,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[13,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[13,10] <- 1
distance[13,12] <- length(index$Long)
distance[13,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[13,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[13,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[13,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

##leaflet(data = distance) %>%
  #addTiles() %>% 
  #addAwesomeMarkers(popup = ~Secteur)
distance[c(8:13),4] <- "Maïs"
distance[,11] <- round(sqrt((distance$Longitude - Long_centre)^2 + (distance$Latitude - Lat_centre)^2),4)
```

```{r Culture de plantes fourragères (0135), echo=FALSE, message=FALSE,warning=FALSE}
df_0135 <- Req::Req_data(industry = 0135, active = 1)%>%
  filter(province == "Quebec") ## On récupères les données de la REQ par API
```

```{r Culture de plantes fourragères (0135) 2, echo=FALSE, message = FALSE, warning = FALSE}

distance[14,1] <- "Plantes fourragères"
distance[14,2] <- round(mean(df_0135$Long,na.rm = TRUE),5)
distance[14,3] <- round(mean(df_0135$Lat,na.rm = TRUE),5) ## On ajoute les coordonnées des centroïdes du secteur d'activité dans la matrice de distance
distance[14,5] <- 1 
distance[14,12] <- length(df_0135$Long)
distance[14,13] <- round(mean(sqrt((df_0135$Lat - mean(df_0135$Lat,na.rm = TRUE))^2 + (df_0135$Long - mean(df_0135$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[14,14] <- round(sd(sqrt((df_0135$Lat - mean(df_0135$Lat,na.rm = TRUE))^2 + (df_0135$Long - mean(df_0135$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[14,15] <- round(sd(sqrt((df_0135$Lat - ecosysteme$Latitude[1])^2 + (df_0135$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[14,16] <- round(mean(sqrt((df_0135$Long - ecosysteme$Longitude)^2 + (df_0135$Lat - ecosysteme$Latitude)^2)),4)


index <- df_0135 %>%
  filter(COD_INTVAL_EMPLO_QUE != "N" & COD_INTVAL_EMPLO_QUE != "O"
         & COD_INTVAL_EMPLO_QUE != "A" 
         & COD_INTVAL_EMPLO_QUE != "B" )
distance[15,1] <- "Plantes fourragères & emp > 10" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 10 employés
distance[15,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[15,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[15,6] <- 1 
distance[15,12] <- length(index$Long)
distance[15,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[15,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[15,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[15,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0135 %>%
  filter(COD_INTVAL_EMPLO_QUE != "E" & COD_INTVAL_EMPLO_QUE != "D" & COD_INTVAL_EMPLO_QUE != "C")
distance[16,1] <- "Plantes fourragères & emp < 10" ## On ajoute la catégorie des entreprises agricoles céréalières de moins de 10 employés
distance[16,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[16,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[16,7] <- 1 
distance[16,12] <- length(index$Long)
distance[16,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[16,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[16,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[16,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0135 %>%
  filter(DAT_STAT_IMMAT > "2018-01-01")
distance[17,1] <- "Plantes fourragères & age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans
distance[17,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[17,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[17,8] <- 1 
distance[17,12] <- length(index$Long)
distance[17,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[17,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[17,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[17,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0135 %>%
  filter(DAT_STAT_IMMAT < "2018-01-01" & DAT_STAT_IMMAT > "2008-01-01" )
distance[18,1] <- "Plantes fourragères & 15 < age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans et moins de 15 ans
distance[18,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[18,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[18,9] <- 1 
distance[18,12] <- length(index$Long)
distance[18,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[18,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[18,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[18,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0135 %>%
  filter(DAT_STAT_IMMAT < "2008-01-01")
distance[19,1] <- "Plantes fourragères & age > 15" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 15 ans
distance[19,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[19,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[19,10] <- 1 
distance[19,12] <- length(index$Long)
distance[19,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[19,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[19,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[19,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

##leaflet(data = distance) %>%
  #addTiles() %>% 
  #addAwesomeMarkers(popup = ~Secteur)
distance[c(14:19),4] <- "Plantes fourragères"
distance[,11] <- round(sqrt((distance$Longitude - Long_centre)^2 + (distance$Latitude - Lat_centre)^2),4)
```

```{r Culture de pommes de terre (0138) , echo=FALSE, message=FALSE,warning=FALSE}
df_0138 <- Req::Req_data(industry = 0138, active = 1)%>%
  filter(province == "Quebec") ## On récupères les données de la REQ par API
```

```{r Culture de pommes de terre (0138) 2, echo=FALSE, message = FALSE, warning = FALSE}

distance[20,1] <- "Pommes de terre"
distance[20,2] <- round(mean(df_0138$Long,na.rm = TRUE),5)
distance[20,3] <- round(mean(df_0138$Lat,na.rm = TRUE),5) ## On ajoute les coordonnées des centroïdes du secteur d'activité dans la matrice de distance
distance[20,5] <- 1
distance[20,12] <- length(df_0138$Long)
distance[20,13] <- round(mean(sqrt((df_0138$Lat - mean(df_0138$Lat,na.rm = TRUE))^2 + (df_0138$Long - mean(df_0138$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[20,14] <- round(sd(sqrt((df_0138$Lat - mean(df_0138$Lat,na.rm = TRUE))^2 + (df_0138$Long - mean(df_0138$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[20,15] <- round(sd(sqrt((df_0138$Lat - ecosysteme$Latitude[1])^2 + (df_0138$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[20,16] <- round(mean(sqrt((df_0138$Long - ecosysteme$Longitude)^2 + (df_0138$Lat - ecosysteme$Latitude)^2)),4)


index <- df_0138 %>%
  filter(COD_INTVAL_EMPLO_QUE != "N" & COD_INTVAL_EMPLO_QUE != "O"
         & COD_INTVAL_EMPLO_QUE != "A" 
         & COD_INTVAL_EMPLO_QUE != "B" )
distance[21,1] <- "Pommes de terre & emp > 10" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 10 employés
distance[21,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[21,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[21,6] <- 1
distance[21,12] <- length(index$Long)
distance[21,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[21,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[21,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[21,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0138 %>%
  filter(COD_INTVAL_EMPLO_QUE != "E" & COD_INTVAL_EMPLO_QUE != "D" & COD_INTVAL_EMPLO_QUE != "C")
distance[22,1] <- "Pommes de terre & emp < 10" ## On ajoute la catégorie des entreprises agricoles céréalières de moins de 10 employés
distance[22,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[22,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[22,7] <- 1
distance[22,12] <- length(index$Long)
distance[22,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[22,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[22,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[22,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0138 %>%
  filter(DAT_STAT_IMMAT > "2018-01-01")
distance[23,1] <- "Pommes de terre & age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans
distance[23,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[23,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[23,8] <- 1
distance[23,12] <- length(index$Long)
distance[23,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[23,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[23,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[23,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0138 %>%
  filter(DAT_STAT_IMMAT < "2018-01-01" & DAT_STAT_IMMAT > "2008-01-01" )
distance[24,1] <- "Pommes de terre & 15 < age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans et moins de 15 ans
distance[24,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[24,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[24,9] <- 1
distance[24,12] <- length(index$Long)
distance[24,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[24,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[24,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[24,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0138 %>%
  filter(DAT_STAT_IMMAT < "2008-01-01")
distance[25,1] <- "Pommes de terre & age > 15" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 15 ans
distance[25,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[25,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[25,10] <- 1
distance[25,12] <- length(index$Long)
distance[25,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[25,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[25,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[25,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

##leaflet(data = distance) %>%
  #addTiles() %>% 
  #addAwesomeMarkers(popup = ~Secteur)
distance[c(20:25),4] <- "Pommes de terre"
distance[,11] <- round(sqrt((distance$Longitude - Long_centre)^2 + (distance$Latitude - Lat_centre)^2),4)
```

```{r Autres grandes cultures (0139) , echo=FALSE, message=FALSE,warning=FALSE}
df_0139 <- Req::Req_data(industry = 0139, active = 1) %>%
  filter(province == "Quebec") ## On récupères les données de la REQ par API
```

```{r Autres grandes cultures (0139) 2, echo=FALSE, message = FALSE, warning = FALSE}

distance[26,1] <- "Autres grandes cultures"
distance[26,2] <- round(mean(df_0139$Long,na.rm = TRUE),5)
distance[26,3] <- round(mean(df_0139$Lat,na.rm = TRUE),5) ## On ajoute les coordonnées des centroïdes du secteur d'activité dans la matrice de distance
distance[26,5] <- 1
distance[26,12] <- length(df_0139$Long)
distance[26,13] <- round(mean(sqrt((df_0139$Lat - mean(df_0139$Lat,na.rm = TRUE))^2 + (df_0139$Long - mean(df_0139$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[26,14] <- round(sd(sqrt((df_0139$Lat - mean(df_0139$Lat,na.rm = TRUE))^2 + (df_0139$Long - mean(df_0139$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[26,15] <- round(sd(sqrt((df_0139$Lat - ecosysteme$Latitude[1])^2 + (df_0139$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[26,16] <- round(mean(sqrt((df_0139$Long - ecosysteme$Longitude)^2 + (df_0139$Lat - ecosysteme$Latitude)^2)),4)
  
index <- df_0139 %>%
  filter(COD_INTVAL_EMPLO_QUE != "N" & COD_INTVAL_EMPLO_QUE != "O"
         & COD_INTVAL_EMPLO_QUE != "A" 
         & COD_INTVAL_EMPLO_QUE != "B" )
distance[27,1] <- "Autres grandes cultures & emp > 10" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 10 employés
distance[27,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[27,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[27,6] <- 1
distance[27,12] <- length(index$Long)
distance[27,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[27,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[27,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[27,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0139 %>%
  filter(COD_INTVAL_EMPLO_QUE != "E" & COD_INTVAL_EMPLO_QUE != "D" & COD_INTVAL_EMPLO_QUE != "C")
distance[28,1] <- "Autres grandes cultures & emp < 10" ## On ajoute la catégorie des entreprises agricoles céréalières de moins de 10 employés
distance[28,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[28,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[28,7] <- 1
distance[28,12] <- length(index$Long)
distance[28,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[28,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[28,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[28,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0139 %>%
  filter(DAT_STAT_IMMAT > "2018-01-01")
distance[29,1] <- "Autres grandes cultures & age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans
distance[29,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[29,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[29,8] <- 1
distance[29,12] <- length(index$Long)
distance[29,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[29,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[29,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[29,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0139 %>%
  filter(DAT_STAT_IMMAT < "2018-01-01" & DAT_STAT_IMMAT > "2008-01-01" )
distance[30,1] <- "Autres grandes cultures & 15 < age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans et moins de 15 ans
distance[30,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[30,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[30,9] <- 1
distance[30,12] <- length(index$Long)
distance[30,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[30,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[30,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[30,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0139 %>%
  filter(DAT_STAT_IMMAT < "2008-01-01")
distance[31,1] <- "Autres grandes cultures & age > 15" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 15 ans
distance[31,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[31,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[31,10] <- 1
distance[31,12] <- length(index$Long)
distance[31,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[31,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[31,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[31,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

##leaflet(data = distance) %>%
  #addTiles() %>% 
  #addAwesomeMarkers(popup = ~Secteur)
distance[c(26:31),4] <- "Autres grandes cultures"
distance[,11] <- round(sqrt((distance$Longitude - Long_centre)^2 + (distance$Latitude - Lat_centre)^2),4)
```

```{r Culture de fruits (0151) , echo=FALSE, message=FALSE,warning=FALSE}
df_0151 <- Req::Req_data(industry = 0151, active = 1) %>%
  filter(province == "Quebec") ## On récupères les données de la REQ par API
```

```{r Culture de fruits (0151) 2, echo=FALSE, message = FALSE, warning = FALSE}

distance[32,1] <- "Fruits"
distance[32,2] <- round(mean(df_0151$Long,na.rm = TRUE),5)
distance[32,3] <- round(mean(df_0151$Lat,na.rm = TRUE),5) ## On ajoute les coordonnées des centroïdes du secteur d'activité dans la matrice de distance
distance[32,5] <- 1
distance[32,12] <- length(df_0151$Long)
distance[32,13] <- round(mean(sqrt((df_0151$Lat - mean(df_0151$Lat,na.rm = TRUE))^2 + (df_0151$Long - mean(df_0151$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[32,14] <- round(sd(sqrt((df_0151$Lat - mean(df_0151$Lat,na.rm = TRUE))^2 + (df_0151$Long - mean(df_0151$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[32,15] <- round(sd(sqrt((df_0151$Lat - ecosysteme$Latitude[1])^2 + (df_0151$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[32,16] <- round(mean(sqrt((df_0151$Long - ecosysteme$Longitude)^2 + (df_0151$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0151 %>%
  filter(COD_INTVAL_EMPLO_QUE != "N" & COD_INTVAL_EMPLO_QUE != "O"
         & COD_INTVAL_EMPLO_QUE != "A" 
         & COD_INTVAL_EMPLO_QUE != "B" )
distance[33,1] <- "Fruits & emp > 10" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 10 employés
distance[33,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[33,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[33,6] <- 1
distance[33,12] <- length(index$Long)
distance[33,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[33,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[33,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[33,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0151 %>%
  filter(COD_INTVAL_EMPLO_QUE != "E" & COD_INTVAL_EMPLO_QUE != "D" & COD_INTVAL_EMPLO_QUE != "C")
distance[34,1] <- "Fruits & emp < 10" ## On ajoute la catégorie des entreprises agricoles céréalières de moins de 10 employés
distance[34,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[34,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[34,7] <- 1
distance[34,12] <- length(index$Long)
distance[34,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[34,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[34,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[34,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0151 %>%
  filter(DAT_STAT_IMMAT > "2018-01-01")
distance[35,1] <- "Fruits & age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans
distance[35,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[35,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[35,8] <- 1
distance[35,12] <- length(index$Long)
distance[35,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[35,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[35,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[35,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0151 %>%
  filter(DAT_STAT_IMMAT < "2018-01-01" & DAT_STAT_IMMAT > "2008-01-01" )
distance[36,1] <- "Fruits & 15 < age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans et moins de 15 ans
distance[36,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[36,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[36,9] <- 1
distance[36,12] <- length(index$Long)
distance[36,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[36,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[36,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[36,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0151 %>%
  filter(DAT_STAT_IMMAT < "2008-01-01")
distance[37,1] <- "Fruits & age > 15" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 15 ans
distance[37,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[37,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[37,10] <- 1
distance[37,12] <- length(index$Long)
distance[37,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[37,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[37,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[37,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

##leaflet(data = distance) %>%
  #addTiles() %>% 
  #addAwesomeMarkers(popup = ~Secteur)
distance[c(32:37),4] <- "Fruits"
distance[,11] <- round(sqrt((distance$Longitude - Long_centre)^2 + (distance$Latitude - Lat_centre)^2),4)
```

```{r Culture de légumes (0152) , echo=FALSE, message=FALSE,warning=FALSE}
df_0152 <- Req::Req_data(industry = 0152, active = 1) %>%
  filter(province == "Quebec") ## On récupères les données de la REQ par API
```

```{r Culture de légumes (0152) 2, echo=FALSE, message = FALSE, warning = FALSE}

distance[38,1] <- "Légumes"
distance[38,2] <- round(mean(df_0152$Long,na.rm = TRUE),5)
distance[38,3] <- round(mean(df_0152$Lat,na.rm = TRUE),5) ## On ajoute les coordonnées des centroïdes du secteur d'activité dans la matrice de distance
distance[38,5] <- 1 
distance[38,12] <- length(df_0152$Long)
distance[38,13] <- round(mean(sqrt((df_0152$Lat - mean(df_0152$Lat,na.rm = TRUE))^2 + (df_0152$Long - mean(df_0152$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[38,14] <- round(sd(sqrt((df_0152$Lat - mean(df_0152$Lat,na.rm = TRUE))^2 + (df_0152$Long - mean(df_0152$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[38,15] <- round(sd(sqrt((df_0152$Lat - ecosysteme$Latitude[1])^2 + (df_0152$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[38,16] <- round(mean(sqrt((df_0152$Long - ecosysteme$Longitude)^2 + (df_0152$Lat - ecosysteme$Latitude)^2)),4)


index <- df_0152 %>%
  filter(COD_INTVAL_EMPLO_QUE != "N" & COD_INTVAL_EMPLO_QUE != "O"
         & COD_INTVAL_EMPLO_QUE != "A" 
         & COD_INTVAL_EMPLO_QUE != "B" )
distance[39,1] <- "Légumes & emp > 10" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 10 employés
distance[39,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[39,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[39,6] <- 1 
distance[39,12] <- length(index$Long)
distance[39,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[39,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[39,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[39,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0152 %>%
  filter(COD_INTVAL_EMPLO_QUE != "E" & COD_INTVAL_EMPLO_QUE != "D" & COD_INTVAL_EMPLO_QUE != "C")
distance[40,1] <- "Légumes & emp < 10" ## On ajoute la catégorie des entreprises agricoles céréalières de moins de 10 employés
distance[40,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[40,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[40,7] <- 1 
distance[40,12] <- length(index$Long)
distance[40,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[40,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[40,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[40,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0152 %>%
  filter(DAT_STAT_IMMAT > "2018-01-01")
distance[41,1] <- "Légumes & age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans
distance[41,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[41,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[41,8] <- 1 
distance[41,12] <- length(index$Long)
distance[41,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[41,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[41,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[41,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)


index <- df_0152 %>%
  filter(DAT_STAT_IMMAT < "2018-01-01" & DAT_STAT_IMMAT > "2008-01-01" )
distance[42,1] <- "Légumes & 15 < age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans et moins de 15 ans
distance[42,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[42,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[42,9] <- 1 
distance[42,12] <- length(index$Long)
distance[42,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[42,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[42,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[42,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)


index <- df_0152 %>%
  filter(DAT_STAT_IMMAT < "2008-01-01")
distance[43,1] <- "Légumes & age > 15" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 15 ans
distance[43,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[43,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[43,10] <- 1 
distance[43,12] <- length(index$Long)
distance[43,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[43,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[43,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[43,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)


##leaflet(data = distance) %>%
  #addTiles() %>% 
  #addAwesomeMarkers(popup = ~Secteur)
distance[c(38:43),4] <- "Légumes"
distance[,11] <- round(sqrt((distance$Longitude - Long_centre)^2 + (distance$Latitude - Lat_centre)^2),4)
```

```{r Culture mixte de fruits et légumes (0159) , echo=FALSE, message=FALSE,warning=FALSE}
df_0159 <- Req::Req_data(industry = 0159, active = 1) %>%
  filter(province == "Quebec") ## On récupères les données de la REQ par API
```

```{r Culture mixte de fruits et légumes (0159) 2, echo=FALSE, message = FALSE, warning = FALSE}

distance[44,1] <- "Mixte fruits et légumes"
distance[44,2] <- round(mean(df_0159$Long,na.rm = TRUE),5)
distance[44,3] <- round(mean(df_0159$Lat,na.rm = TRUE),5) ## On ajoute les coordonnées des centroïdes du secteur d'activité dans la matrice de distance
distance[44,5] <- 1 
distance[44,12] <- length(df_0159$Long)
distance[44,13] <- round(mean(sqrt((df_0159$Lat - mean(df_0159$Lat,na.rm = TRUE))^2 + (df_0159$Long - mean(df_0159$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[44,14] <- round(sd(sqrt((df_0159$Lat - mean(df_0159$Lat,na.rm = TRUE))^2 + (df_0159$Long - mean(df_0159$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[44,15] <- round(sd(sqrt((df_0159$Lat - ecosysteme$Latitude[1])^2 + (df_0159$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[44,16] <- round(mean(sqrt((df_0159$Long - ecosysteme$Longitude)^2 + (df_0159$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0159 %>%
  filter(COD_INTVAL_EMPLO_QUE != "N" & COD_INTVAL_EMPLO_QUE != "O"
         & COD_INTVAL_EMPLO_QUE != "A" 
         & COD_INTVAL_EMPLO_QUE != "B" )
distance[45,1] <- "Mixte fruits et légumes & emp > 10" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 10 employés
distance[45,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[45,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[45,6] <- 1 
distance[45,12] <- length(index$Long)
distance[45,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[45,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[45,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[45,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)


index <- df_0159 %>%
  filter(COD_INTVAL_EMPLO_QUE != "E" & COD_INTVAL_EMPLO_QUE != "D" & COD_INTVAL_EMPLO_QUE != "C")
distance[46,1] <- "Mixte fruits et légumes & emp < 10" ## On ajoute la catégorie des entreprises agricoles céréalières de moins de 10 employés
distance[46,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[46,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[46,7] <- 1 
distance[46,12] <- length(index$Long)
distance[46,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[46,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[46,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[46,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)


index <- df_0159 %>%
  filter(DAT_STAT_IMMAT > "2018-01-01")
distance[47,1] <- "Mixte fruits et légumes & age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans
distance[47,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[47,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[47,8] <- 1 
distance[47,12] <- length(index$Long)
distance[47,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[47,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[47,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[47,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)


index <- df_0159 %>%
  filter(DAT_STAT_IMMAT < "2018-01-01" & DAT_STAT_IMMAT > "2008-01-01" )
distance[48,1] <- "Mixte fruits et légumes & 15 < age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans et moins de 15 ans
distance[48,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[48,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[48,9] <- 1 
distance[48,12] <- length(index$Long)
distance[48,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[48,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[48,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[48,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)


index <- df_0159 %>%
  filter(DAT_STAT_IMMAT < "2008-01-01")
distance[49,1] <- "Mixte fruits et légumes & age > 15" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 15 ans
distance[49,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[49,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[49,10] <- 1 
distance[49,12] <- length(index$Long)
distance[49,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[49,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[49,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[49,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)


##leaflet(data = distance) %>%
  #addTiles() %>% 
  #addAwesomeMarkers(popup = ~Secteur)
distance[c(44:49),4] <- "Mixte fruits et légumes"
distance[,11] <- round(sqrt((distance$Longitude - Long_centre)^2 + (distance$Latitude - Lat_centre)^2),4)
```

```{r Culture de champignons (0161) , echo=FALSE, message=FALSE,warning=FALSE}
df_0161 <- Req::Req_data(industry = 0161, active = 1) %>%
  filter(province == "Quebec") ## On récupères les données de la REQ par API
```

```{r Culture de champignons (0161) 2, echo=FALSE, message = FALSE, warning = FALSE}

distance[50,1] <- "Champignons"
distance[50,2] <- round(mean(df_0161$Long,na.rm = TRUE),5)
distance[50,3] <- round(mean(df_0161$Lat,na.rm = TRUE),5) ## On ajoute les coordonnées des centroïdes du secteur d'activité dans la matrice de distance
distance[50,5] <- 1
distance[50,12] <- length(df_0161$Long)
distance[50,13] <- round(mean(sqrt((df_0161$Lat - mean(df_0161$Lat,na.rm = TRUE))^2 + (df_0161$Long - mean(df_0161$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[50,14] <- round(sd(sqrt((df_0161$Lat - mean(df_0161$Lat,na.rm = TRUE))^2 + (df_0161$Long - mean(df_0161$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[50,15] <- round(sd(sqrt((df_0161$Lat - ecosysteme$Latitude[1])^2 + (df_0161$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[50,16] <- round(mean(sqrt((df_0161$Long - ecosysteme$Longitude)^2 + (df_0161$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0161 %>%
  filter(COD_INTVAL_EMPLO_QUE != "N" & COD_INTVAL_EMPLO_QUE != "O"
         & COD_INTVAL_EMPLO_QUE != "A" 
         & COD_INTVAL_EMPLO_QUE != "B" )
distance[51,1] <- "Champignons & emp > 10" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 10 employés
distance[51,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[51,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[51,6] <- 1
distance[51,12] <- length(index$Long)
distance[51,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[51,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[51,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[51,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)


index <- df_0161 %>%
  filter(COD_INTVAL_EMPLO_QUE != "E" & COD_INTVAL_EMPLO_QUE != "D" & COD_INTVAL_EMPLO_QUE != "C")
distance[52,1] <- "Champignons & emp < 10" ## On ajoute la catégorie des entreprises agricoles céréalières de moins de 10 employés
distance[52,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[52,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[52,7] <- 1
distance[52,12] <- length(index$Long)
distance[52,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[52,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[52,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[52,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0161 %>%
  filter(DAT_STAT_IMMAT > "2018-01-01")
distance[53,1] <- "Champignons & age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans
distance[53,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[53,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[53,8] <- 1
distance[53,12] <- length(index$Long)
distance[53,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[53,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[53,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[53,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0161 %>%
  filter(DAT_STAT_IMMAT < "2018-01-01" & DAT_STAT_IMMAT > "2008-01-01" )
distance[54,1] <- "Champignons & 15 < age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans et moins de 15 ans
distance[54,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[54,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[54,9] <- 1
distance[54,12] <- length(index$Long)
distance[54,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[54,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[54,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[54,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0161 %>%
  filter(DAT_STAT_IMMAT < "2008-01-01")
distance[55,1] <- "Champignons & age > 15" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 15 ans
distance[55,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[55,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[55,10] <- 1
distance[55,12] <- length(index$Long)
distance[55,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[55,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[55,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[55,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

##leaflet(data = distance) %>%
  #addTiles() %>% 
  #addAwesomeMarkers(popup = ~Secteur)
distance[c(50:55),4] <- "Champignons"
distance[,11] <- round(sqrt((distance$Longitude - Long_centre)^2 + (distance$Latitude - Lat_centre)^2),4)
```

```{r Culture en serres (0162) , echo=FALSE, message=FALSE,warning=FALSE}
df_0162 <- Req::Req_data(industry = 0162, active = 1) %>%
  filter(province == "Quebec") ## On récupères les données de la REQ par API
```

```{r Culture en serres (0162) 2, echo=FALSE, message = FALSE, warning = FALSE}

distance[56,1] <- "Serres"
distance[56,2] <- round(mean(df_0162$Long,na.rm = TRUE),5)
distance[56,3] <- round(mean(df_0162$Lat,na.rm = TRUE),5) ## On ajoute les coordonnées des centroïdes du secteur d'activité dans la matrice de distance
distance[56,5] <- 1
distance[56,12] <- length(df_0162$Long)
distance[56,13] <- round(mean(sqrt((df_0162$Lat - mean(df_0162$Lat,na.rm = TRUE))^2 + (df_0162$Long - mean(df_0162$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[56,14] <- round(sd(sqrt((df_0162$Lat - mean(df_0162$Lat,na.rm = TRUE))^2 + (df_0162$Long - mean(df_0162$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[56,15] <- round(sd(sqrt((df_0162$Lat - ecosysteme$Latitude[1])^2 + (df_0162$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[56,16] <- round(mean(sqrt((df_0162$Long - ecosysteme$Longitude)^2 + (df_0162$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0162 %>%
  filter(COD_INTVAL_EMPLO_QUE != "N" & COD_INTVAL_EMPLO_QUE != "O"
         & COD_INTVAL_EMPLO_QUE != "A" 
         & COD_INTVAL_EMPLO_QUE != "B" )
distance[57,1] <- "Serres & emp > 10" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 10 employés
distance[57,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[57,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[57,6] <- 1
distance[57,12] <- length(index$Long)
distance[57,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[57,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[57,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[57,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0162 %>%
  filter(COD_INTVAL_EMPLO_QUE != "E" & COD_INTVAL_EMPLO_QUE != "D" & COD_INTVAL_EMPLO_QUE != "C")
distance[58,1] <- "Serres & emp < 10" ## On ajoute la catégorie des entreprises agricoles céréalières de moins de 10 employés
distance[58,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[58,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[58,7] <- 1
distance[58,12] <- length(index$Long)
distance[58,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[58,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[58,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[58,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0162 %>%
  filter(DAT_STAT_IMMAT > "2018-01-01")
distance[59,1] <- "Serres & age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans
distance[59,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[59,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[59,8] <- 1
distance[59,12] <- length(index$Long)
distance[59,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[59,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[59,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[59,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0162 %>%
  filter(DAT_STAT_IMMAT < "2018-01-01" & DAT_STAT_IMMAT > "2008-01-01" )
distance[60,1] <- "Serres & 15 < age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans et moins de 15 ans
distance[60,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[60,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[60,9] <- 1
distance[60,12] <- length(index$Long)
distance[60,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[60,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[60,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[60,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0162 %>%
  filter(DAT_STAT_IMMAT < "2008-01-01")
distance[61,1] <- "Serres & age > 15" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 15 ans
distance[61,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[61,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[61,10] <- 1
distance[61,12] <- length(index$Long)
distance[61,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[61,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[61,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[61,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

##leaflet(data = distance) %>%
  #addTiles() %>% 
  #addAwesomeMarkers(popup = ~Secteur)
distance[c(56:61),4] <- "Serres"
distance[,11] <- round(sqrt((distance$Longitude - Long_centre)^2 + (distance$Latitude - Lat_centre)^2),4)
```

```{r Culture en pépinières & gazonnières (0163) , echo=FALSE, message=FALSE,warning=FALSE}
df_0163 <- Req::Req_data(industry = 0163, active = 1) %>%
  filter(province == "Quebec") ## On récupères les données de la REQ par API
```

```{r Culture en pépinières & gazonnières (0163) 2, echo=FALSE, message = FALSE, warning = FALSE}

distance[62,1] <- "Pépinières"
distance[62,2] <- round(mean(df_0163$Long,na.rm = TRUE),5)
distance[62,3] <- round(mean(df_0163$Lat,na.rm = TRUE),5) ## On ajoute les coordonnées des centroïdes du secteur d'activité dans la matrice de distance
distance[62,5] <- 1
distance[62,12] <- length(df_0163$Long)
distance[62,13] <- round(mean(sqrt((df_0163$Lat - mean(df_0163$Lat,na.rm = TRUE))^2 + (df_0163$Long - mean(df_0163$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[62,14] <- round(sd(sqrt((df_0163$Lat - mean(df_0163$Lat,na.rm = TRUE))^2 + (df_0163$Long - mean(df_0163$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[62,15] <- round(sd(sqrt((df_0163$Lat - ecosysteme$Latitude[1])^2 + (df_0163$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[62,16] <- round(mean(sqrt((df_0163$Long - ecosysteme$Longitude)^2 + (df_0163$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0163 %>%
  filter(COD_INTVAL_EMPLO_QUE != "N" & COD_INTVAL_EMPLO_QUE != "O"
         & COD_INTVAL_EMPLO_QUE != "A" 
         & COD_INTVAL_EMPLO_QUE != "B" )
distance[63,1] <- "Pépinières & emp > 10" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 10 employés
distance[63,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[63,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[63,6] <- 1
distance[63,12] <- length(index$Long)
distance[63,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[63,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[63,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[63,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0163 %>%
  filter(COD_INTVAL_EMPLO_QUE != "E" & COD_INTVAL_EMPLO_QUE != "D" & COD_INTVAL_EMPLO_QUE != "C")
distance[64,1] <- "Pépinières & emp < 10" ## On ajoute la catégorie des entreprises agricoles céréalières de moins de 10 employés
distance[64,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[64,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[64,7] <- 1
distance[64,12] <- length(index$Long)
distance[64,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[64,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[64,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[64,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0163 %>%
  filter(DAT_STAT_IMMAT > "2018-01-01")
distance[65,1] <- "Pépinières & age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans
distance[65,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[65,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[65,8] <- 1
distance[65,12] <- length(index$Long)
distance[65,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[65,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[65,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[65,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0163 %>%
  filter(DAT_STAT_IMMAT < "2018-01-01" & DAT_STAT_IMMAT > "2008-01-01" )
distance[66,1] <- "Pépinières & 15 < age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans et moins de 15 ans
distance[66,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[66,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[66,9] <- 1
distance[66,12] <- length(index$Long)
distance[66,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[66,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[66,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[66,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0163 %>%
  filter(DAT_STAT_IMMAT < "2008-01-01")
distance[67,1] <- "Pépinières & age > 15" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 15 ans
distance[67,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[67,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[67,10] <- 1
distance[67,12] <- length(index$Long)
distance[67,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[67,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[67,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[67,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

##leaflet(data = distance) %>%
  #addTiles() %>% 
  #addAwesomeMarkers(popup = ~Secteur)
distance[c(62:67),4] <- "Pépinières"
distance[,11] <- round(sqrt((distance$Longitude - Long_centre)^2 + (distance$Latitude - Lat_centre)^2),4)
```

```{r Culture érabilières (0164) , echo=FALSE, message=FALSE,warning=FALSE}
df_0164 <- Req::Req_data(industry = 0164, active = 1) %>%
  filter(province == "Quebec") ## On récupères les données de la REQ par API
```

```{r Culture érabilières (0164) 2, echo=FALSE, message = FALSE, warning = FALSE}

distance[68,1] <- "Érablières"
distance[68,2] <- round(mean(df_0164$Long,na.rm = TRUE),5)
distance[68,3] <- round(mean(df_0164$Lat,na.rm = TRUE),5) ## On ajoute les coordonnées des centroïdes du secteur d'activité dans la matrice de distance
distance[68,5] <- 1
distance[68,12] <- length(df_0164$Long)
distance[68,13] <- round(mean(sqrt((df_0164$Lat - mean(df_0164$Lat,na.rm = TRUE))^2 + (df_0164$Long - mean(df_0164$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[68,14] <- round(sd(sqrt((df_0164$Lat - mean(df_0164$Lat,na.rm = TRUE))^2 + (df_0164$Long - mean(df_0164$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[68,15] <- round(sd(sqrt((df_0164$Lat - ecosysteme$Latitude[1])^2 + (df_0164$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[68,16] <- round(mean(sqrt((df_0164$Long - ecosysteme$Longitude)^2 + (df_0164$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0164 %>%
  filter(COD_INTVAL_EMPLO_QUE != "N" & COD_INTVAL_EMPLO_QUE != "O"
         & COD_INTVAL_EMPLO_QUE != "A" 
         & COD_INTVAL_EMPLO_QUE != "B" )
distance[69,1] <- "Érablières & emp > 10" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 10 employés
distance[69,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[69,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[69,6] <- 1
distance[69,12] <- length(index$Long)
distance[69,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[69,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[69,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[69,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0164 %>%
  filter(COD_INTVAL_EMPLO_QUE != "E" & COD_INTVAL_EMPLO_QUE != "D" & COD_INTVAL_EMPLO_QUE != "C")
distance[70,1] <- "Érablières & emp < 10" ## On ajoute la catégorie des entreprises agricoles céréalières de moins de 10 employés
distance[70,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[70,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[70,7] <- 1
distance[70,12] <- length(index$Long)
distance[70,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[70,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[70,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[70,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0164 %>%
  filter(DAT_STAT_IMMAT > "2018-01-01")
distance[71,1] <- "Érablières & age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans
distance[71,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[71,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[71,8] <- 1
distance[71,12] <- length(index$Long)
distance[71,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[71,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[71,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[71,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0164 %>%
  filter(DAT_STAT_IMMAT < "2018-01-01" & DAT_STAT_IMMAT > "2008-01-01" )
distance[72,1] <- "Érablières & 15 < age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans et moins de 15 ans
distance[72,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[72,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[72,9] <- 1
distance[72,12] <- length(index$Long)
distance[72,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[72,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[72,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[72,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0164 %>%
  filter(DAT_STAT_IMMAT < "2008-01-01")
distance[73,1] <- "Érablières & age > 15" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 15 ans
distance[73,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[73,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[73,10] <- 1
distance[73,12] <- length(index$Long)
distance[73,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[73,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[73,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[73,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

##leaflet(data = distance) %>%
  #addTiles() %>% 
  #addAwesomeMarkers(popup = ~Secteur)
distance[c(68:73),4] <- "Érablières"
distance[,11] <- round(sqrt((distance$Longitude - Long_centre)^2 + (distance$Latitude - Lat_centre)^2),4)
```

```{r Autres spécialités horticoles (0169) , echo=FALSE, message=FALSE,warning=FALSE}
df_0169 <- Req::Req_data(industry = 0169, active = 1) %>%
  filter(province == "Quebec") ## On récupères les données de la REQ par API

```

```{r Autres spécialités horticoles (0169) 2, echo=FALSE, message = FALSE, warning = FALSE}

distance[74,1] <- "Autres cultures horticoles"
distance[74,2] <- round(mean(df_0169$Long,na.rm = TRUE),5)
distance[74,3] <- round(mean(df_0169$Lat,na.rm = TRUE),5) ## On ajoute les coordonnées des centroïdes du secteur d'activité dans la matrice de distance
distance[74,5] <- 1
distance[74,12] <- length(df_0169$Long)
distance[74,13] <- round(mean(sqrt((df_0169$Lat - mean(df_0169$Lat,na.rm = TRUE))^2 + (df_0169$Long - mean(df_0169$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[74,14] <- round(sd(sqrt((df_0169$Lat - mean(df_0169$Lat,na.rm = TRUE))^2 + (df_0169$Long - mean(df_0169$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[74,15] <- round(sd(sqrt((df_0169$Lat - ecosysteme$Latitude[1])^2 + (df_0169$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[74,16] <- round(mean(sqrt((df_0169$Long - ecosysteme$Longitude)^2 + (df_0169$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0169 %>%
  filter(COD_INTVAL_EMPLO_QUE != "N" & COD_INTVAL_EMPLO_QUE != "O"
         & COD_INTVAL_EMPLO_QUE != "A" 
         & COD_INTVAL_EMPLO_QUE != "B" )
distance[75,1] <- "Autres cultures horticoles & emp > 10" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 10 employés
distance[75,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[75,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[75,6] <- 1
distance[75,12] <- length(index$Long)
distance[75,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[75,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[75,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[75,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0169 %>%
  filter(COD_INTVAL_EMPLO_QUE != "E" & COD_INTVAL_EMPLO_QUE != "D" & COD_INTVAL_EMPLO_QUE != "C")
distance[76,1] <- "Autres cultures horticoles & emp < 10" ## On ajoute la catégorie des entreprises agricoles céréalières de moins de 10 employés
distance[76,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[76,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[76,7] <- 1
distance[76,12] <- length(index$Long)
distance[76,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[76,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[76,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[76,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0169 %>%
  filter(DAT_STAT_IMMAT > "2018-01-01")
distance[77,1] <- "Autres cultures horticoles & age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans
distance[77,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[77,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[77,8] <- 1
distance[77,12] <- length(index$Long)
distance[77,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[77,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[77,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[77,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0169 %>%
  filter(DAT_STAT_IMMAT < "2018-01-01" & DAT_STAT_IMMAT > "2008-01-01" )
distance[78,1] <- "Autres cultures horticoles & 15 < age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans et moins de 15 ans
distance[78,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[78,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[78,9] <- 1
distance[78,12] <- length(index$Long)
distance[78,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[78,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[78,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[78,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0169 %>%
  filter(DAT_STAT_IMMAT < "2008-01-01")
distance[79,1] <- "Autres cultures horticoles & age > 15" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 15 ans
distance[79,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[79,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[79,10] <- 1
distance[79,12] <- length(index$Long)
distance[79,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[79,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[79,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[79,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

##leaflet(data = distance) %>%
  #addTiles() %>% 
  #addAwesomeMarkers(popup = ~Secteur)
distance[c(74:79),4] <- "Autres cultures horticoles"
distance[,11] <- round(sqrt((distance$Longitude - Long_centre)^2 + (distance$Latitude - Lat_centre)^2),4)
```

```{r Élevages grandes cultures et productions horticoles (0171) , echo=FALSE, message=FALSE,warning=FALSE}
df_0171 <- Req::Req_data(industry = 0171, active = 1) %>%
  filter(province == "Quebec") ## On récupères les données de la REQ par API
```

```{r Élevages grandes cultures et productions horticoles (0171) 2, echo=FALSE, message = FALSE, warning = FALSE}

distance[80,1] <- "Élevages, grandes cultures et productions horticoles"
distance[80,2] <- round(mean(df_0171$Long,na.rm = TRUE),5)
distance[80,3] <- round(mean(df_0171$Lat,na.rm = TRUE),5) ## On ajoute les coordonnées des centroïdes du secteur d'activité dans la matrice de distance
distance[80,5] <- 1
distance[80,12] <- length(df_0171$Long)
distance[80,13] <- round(mean(sqrt((df_0171$Lat - mean(df_0171$Lat,na.rm = TRUE))^2 + (df_0171$Long - mean(df_0171$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[80,14] <- round(sd(sqrt((df_0171$Lat - mean(df_0171$Lat,na.rm = TRUE))^2 + (df_0171$Long - mean(df_0171$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[80,15] <- round(sd(sqrt((df_0171$Lat - ecosysteme$Latitude[1])^2 + (df_0171$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[80,16] <- round(mean(sqrt((df_0171$Long - ecosysteme$Longitude)^2 + (df_0171$Lat - ecosysteme$Latitude)^2)),4)


index <- df_0171 %>%
  filter(COD_INTVAL_EMPLO_QUE != "N" & COD_INTVAL_EMPLO_QUE != "O"
         & COD_INTVAL_EMPLO_QUE != "A" 
         & COD_INTVAL_EMPLO_QUE != "B" )
distance[81,1] <- "Élevages, grandes cultures et productions horticoles & emp > 10" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 10 employés
distance[81,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[81,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[81,6] <- 1
distance[81,12] <- length(index$Long)
distance[81,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[81,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[81,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[81,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0171 %>%
  filter(COD_INTVAL_EMPLO_QUE != "E" & COD_INTVAL_EMPLO_QUE != "D" & COD_INTVAL_EMPLO_QUE != "C")
distance[82,1] <- "Élevages, grandes cultures et productions horticoles & emp < 10" ## On ajoute la catégorie des entreprises agricoles céréalières de moins de 10 employés
distance[82,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[82,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[82,7] <- 1
distance[82,12] <- length(index$Long)
distance[82,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[82,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[82,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[82,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0171 %>%
  filter(DAT_STAT_IMMAT > "2018-01-01")
distance[83,1] <- "Élevages, grandes cultures et productions horticoles & age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans
distance[83,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[83,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[83,8] <- 1
distance[83,12] <- length(index$Long)
distance[83,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[83,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[83,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[83,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0171 %>%
  filter(DAT_STAT_IMMAT < "2018-01-01" & DAT_STAT_IMMAT > "2008-01-01" )
distance[84,1] <- "Élevages, grandes cultures et productions horticoles & 15 < age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans et moins de 15 ans
distance[84,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[84,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[84,9] <- 1
distance[84,12] <- length(index$Long)
distance[84,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[84,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[84,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[84,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_0171 %>%
  filter(DAT_STAT_IMMAT < "2008-01-01")
distance[85,1] <- "Élevages, grandes cultures et productions horticoles & age > 15" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 15 ans
distance[85,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[85,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[85,10] <- 1
distance[85,12] <- length(index$Long)
distance[85,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[85,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[85,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[85,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

##leaflet(data = distance) %>%
  #addTiles() %>% 
  #addAwesomeMarkers(popup = ~Secteur)
distance[c(80:85),4] <- "Élev., grandes cult. et prod. hort."
distance[,11] <- round(sqrt((distance$Longitude - Long_centre)^2 + (distance$Latitude - Lat_centre)^2),4)
```

```{r echo=FALSE, message = FALSE, warning = FALSE}
#.sidebar
# selectInput("sect_act", label = "Secteur d'activité",
#            choices = distance$Secteur, selected = distance$Secteur[1])

```

```{r echo=FALSE, message = FALSE, warning = FALSE}

##dist_user <- reactive({distance %>%
  #filter(Secteur ==input$sect_act)
  #})

#renderLeaflet({
 # leaflet(data = distance[which(distance$Secteur == input$sect_act),]) %>%
  #addTiles() %>%
  #addAwesomeMarkers(popup = ~Categorie)
#})
```

```{r df exploitations agricoles cumulees, echo=FALSE, message = FALSE, warning = FALSE}

df_tot <- full_join(df_0131,df_0134)
df_tot <- full_join(df_tot,df_0135)
df_tot <- full_join(df_tot,df_0138)
df_tot <- full_join(df_tot,df_0139)
df_tot <- full_join(df_tot,df_0151)
df_tot <- full_join(df_tot,df_0152)
df_tot <- full_join(df_tot,df_0159)
df_tot <- full_join(df_tot,df_0161)
df_tot <- full_join(df_tot,df_0162)
df_tot <- full_join(df_tot,df_0163)
df_tot <- full_join(df_tot,df_0164)
df_tot <- full_join(df_tot,df_0169)
df_tot <- full_join(df_tot,df_0171)
```

```{r exploitations agricoles cumulees 2, echo=FALSE, message = FALSE, warning = FALSE}

distance[86,1] <- "Total des exploitations agricoles"
distance[86,2] <- round(mean(df_tot$Long,na.rm = TRUE),5)
distance[86,3] <- round(mean(df_tot$Lat,na.rm = TRUE),5) ## On ajoute les coordonnées des centroïdes du secteur d'activité dans la matrice de distance
distance[86,5] <- 1
distance[86,12] <- length(df_tot$Long)
distance[86,13] <- round(mean(sqrt((df_tot$Lat - mean(df_tot$Lat,na.rm = TRUE))^2 + (df_tot$Long - mean(df_tot$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[86,14] <- round(sd(sqrt((df_tot$Lat - mean(df_tot$Lat,na.rm = TRUE))^2 + (df_tot$Long - mean(df_tot$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[86,15] <- round(sd(sqrt((df_tot$Lat - ecosysteme$Latitude[1])^2 + (df_tot$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[86,16] <- round(mean(sqrt((df_tot$Long - ecosysteme$Longitude)^2 + (df_tot$Lat - ecosysteme$Latitude)^2)),4)

index <- df_tot %>%
  filter(COD_INTVAL_EMPLO_QUE != "N" & COD_INTVAL_EMPLO_QUE != "O"
         & COD_INTVAL_EMPLO_QUE != "A" 
         & COD_INTVAL_EMPLO_QUE != "B" )
distance[87,1] <- "Total des exploitations agricoles & emp > 10" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 10 employés
distance[87,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[87,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[87,6] <- 1
distance[87,12] <- length(index$Long)
distance[87,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[87,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[87,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[87,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_tot %>%
  filter(COD_INTVAL_EMPLO_QUE != "E" & COD_INTVAL_EMPLO_QUE != "D" & COD_INTVAL_EMPLO_QUE != "C")
distance[88,1] <- "Total des exploitations agricoles & emp < 10" ## On ajoute la catégorie des entreprises agricoles céréalières de moins de 10 employés
distance[88,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[88,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[88,7] <- 1
distance[88,12] <- length(index$Long)
distance[88,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[88,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[88,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[88,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_tot %>%
  filter(DAT_STAT_IMMAT > "2018-01-01")
distance[89,1] <- "Total des exploitations agricoles & age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans
distance[89,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[89,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[89,8] <- 1
distance[89,12] <- length(index$Long)
distance[89,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[89,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[89,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[89,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_tot %>%
  filter(DAT_STAT_IMMAT < "2018-01-01" & DAT_STAT_IMMAT > "2008-01-01" )
distance[90,1] <- "Total des exploitations agricoles & 15 < age < 5" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 5 ans et moins de 15 ans
distance[90,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[90,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[90,9] <- 1
distance[90,12] <- length(index$Long)
distance[90,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[90,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[90,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[90,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

index <- df_tot %>%
  filter(DAT_STAT_IMMAT < "2008-01-01")
distance[91,1] <- "Total des exploitations agricoles & age > 15" ## On ajoute la catégorie des entreprises agricoles céréalières de plus de 15 ans
distance[91,2] <- round(mean(index$Long,na.rm = TRUE),5)
distance[91,3] <- round(mean(index$Lat,na.rm = TRUE),5)
distance[91,10] <- 1
distance[91,12] <- length(index$Long)
distance[91,13] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance[91,14] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)
distance[91,15] <- round(sd(sqrt((index$Lat - ecosysteme$Latitude[1])^2 + (index$Long - ecosysteme$Longitude[1])^2),na.rm = TRUE),4)
distance[91,16] <- round(mean(sqrt((index$Long - ecosysteme$Longitude)^2 + (index$Lat - ecosysteme$Latitude)^2)),4)

##leaflet(data = distance) %>%
  #addTiles() %>% 
  #addAwesomeMarkers(popup = ~Secteur)
distance[c(86:91),4] <- "Total des exploitations agricoles"
distance[,11] <- round(sqrt((distance$Longitude - Long_centre)^2 + (distance$Latitude - Lat_centre)^2),4)
```


# Cartographie des secteurs

## Column {data-width="570"}

### Cartographie du centre des secteurs agricoles et du signal de l'écosystème d'innovation

```{r, echo=FALSE, message = FALSE, warning = FALSE}

index <- distance[-1,] %>%
  filter(Complet == 1)

leaflet(data = index) %>%
  addTiles() %>% 
  addCircleMarkers(lng = ecosysteme$Longitude[1], lat = ecosysteme$Latitude[1], ## On ajoute le centroïde de l'écosystème
      radius = 15, stroke = FALSE, fillOpacity = 0.9, 
      color = "steelblue", popup = ecosysteme$Nom_ent[1]) %>%
  
    addCircleMarkers(data = index[!index$Secteur %in% "Total des exploitations agricoles",],
                   lng = ~Longitude, lat = ~Latitude, color ="#D7301F",stroke = FALSE, fillOpacity = 0.8,
                   popup = ~paste("Nom :", Categorie, "<br/>","Distance avec l'écosystème", Dist_centre)) %>%
  
  addCircleMarkers(data = index[index$Secteur %in% "Total des exploitations agricoles",],
                   lng = ~Longitude, lat = ~Latitude, color = "#FC8D59" , stroke = FALSE, fillOpacity = 0.9, radius = 12,
                   popup = ~paste("Nom :", Categorie, "<br/>","Distance avec l'écosystème", Dist_centre)) %>%
      addLegend(
    "bottomright",
    title = "Légende",
    colors = c("steelblue","#D7301F","#FC8D59"),
   labels = c("Signal de l'écosystème d'innovation", "Centre des secteurs agricoles","Centre du total des exploitations agricoles"), 
    opacity = 0.9)

```

## Column { data-width="430"}

### Distance du centre des secteurs avec le signal de l'écosystème et nombre d'exploitations associées

```{r echo=FALSE, message = FALSE, warning = FALSE}
datatable(index[,c(1,11,12)],
            rownames = FALSE, 
              options = list(pageLength = 5, scrollX = TRUE,dom = 'tip'), 
              class = 'white-space: nowrap' )
```

### Cartographie de la distance moyenne des entreprises des sous-secteurs à l'écosystème et écart-type associé

```{r echo=FALSE, message = FALSE, warning = FALSE}

nb.cols <- 16
mycolors <- colorRampPalette(brewer.pal(8, "OrRd"))(nb.cols)
mycolors2 <- colorRampPalette(brewer.pal(11, "Spectral"))(nb.cols)

distance <- distance %>%
  mutate(Secteur = case_when(Secteur == "Élevages, grandes cultures et productions horticoles"~"Élev., grandes cult. et prod. hort.", TRUE ~ Secteur))

P <- distance[] %>%
  filter(Complet ==1) %>%
    ggplot() +
    geom_point(aes(moyenne_dist,ecart_type_dist,color=Secteur)) +
    xlab("Distance moyenne au centre du secteur") +
    ylab("Ecart-type au centre du secteur") +
    theme_minimal() +
    scale_color_manual(values = mycolors2)
      
  ggplotly(P)%>% #Ploty du graph
  config(displayModeBar = FALSE)
  
  
```

# Impact du nombre d'employé {data-navmenu="Analyse des variables"}

## Column {data-width = "330"}

### Graphique des distances moyennes des entreprises des secteur à l'écosystème en fonction de l'écart-type

```{r echo=FALSE, message = FALSE, warning = FALSE}

index <- distance[] %>%
  filter(Complet==1) 

p <-  ggplot(data = index) +
    geom_point(aes(dist_moy_centre,ecart_type_eco,color=Secteur)) +
    theme_minimal() +
    ylab("Ecart-type") +
    xlab("Distance moyenne au centre") +
    labs(colour = "Secteur") +
    scale_color_manual(values=mycolors2) +
    theme(axis.text.x = element_text(angle = 90))
    
  ggplotly(p)%>% #Ploty du graph
  config(displayModeBar = FALSE)
```

## Column {.tabset data-width="660"}

### Graphique des distances moyennes en fonction du nombre d'employé

```{r echo=FALSE, message = FALSE, warning = FALSE}
p <-  ggplot() +
    geom_point(data = distance[distance$emp_sup_10==1,],
               aes(Secteur,dist_moy_centre,color="> 10 employés")) +
    geom_point(data = distance[distance$emp_inf_10==1,],
               aes(Secteur,dist_moy_centre,color="< 10 employés")) +
    labs(colour = "Distance moyenne") +
    scale_color_manual(values = c("darkgreen","orange"),
                       labels = c("< 10 employés", "> 10 employés" )) +
    theme_minimal() +
    ylab("Valeur") +
    theme(axis.text.x = element_text(angle = 90))
  ggplotly(p)%>% #Ploty du graph
  config(displayModeBar = FALSE)
```

### Graphique des ecart-types en fonction du nombre d'employé

```{r echo=FALSE, message = FALSE, warning = FALSE}
 p <- ggplot() +
    geom_point(data = distance[distance$emp_sup_10==1,],
               aes(Secteur,ecart_type_eco,color = "> 10 employé" )) +
    geom_point(data = distance[distance$emp_inf_10==1,],
               aes(Secteur,ecart_type_eco,color = "< 10 employés" )) +
    labs(colour = "Écart-type") +
    scale_color_manual(values = c("darkgreen","orange"),
                       labels = c("< 10 employés", "> 10 employés")) +
    theme_minimal() +
    ylab("Valeur") +
    theme(axis.text.x = element_text(angle = 90))
  ggplotly(p)%>% #Ploty du graph
  config(displayModeBar = FALSE)
```

# Impact de l'âge {data-navmenu="Analyse des variables"}

## Rows {data-width = 660}

### Graphique des distances moyennes en fonction de l'âge des exploitations

```{r echo=FALSE, message = FALSE, warning = FALSE}
p<-  ggplot() +
    geom_point(data = distance[distance$age_inf_5==1,],
               aes(Secteur,dist_moy_centre,color="âge > 15 ans")) +
    geom_point(data = distance[distance$age_inf_15_sup_5==1,],
               aes(Secteur,dist_moy_centre,color="5 ans < âge < 15 ans")) +
      geom_point(data = distance[distance$age_sup_15==1,],
               aes(Secteur,dist_moy_centre,color="âge < 5 ans")) +
    labs(colour = "Distance moyenne") +
    scale_color_manual(values = c("red","darkgreen","orange"),
                       labels = c("âge < 5 ans","5 ans < âge < 15 ans", "âge > 15 ans" )) +
    theme_minimal() +
    ylab("Valeur") +
    theme(axis.text.x = element_text(angle = 90))
  ggplotly(p)%>% #Ploty du graph
  config(displayModeBar = FALSE)
```

## Rows {data-width = 660}

### Graphique des écart-types en fonction de l'âge des exploitations

```{r echo=FALSE, message = FALSE, warning = FALSE}

p<-  ggplot() +
    geom_point(data = distance[distance$age_inf_5==1,],
               aes(Secteur,ecart_type_eco,color="âge > 15 ans")) +
    geom_point(data = distance[distance$age_inf_15_sup_5==1,],
               aes(Secteur,ecart_type_eco,color="5 ans < âge < 15 ans")) +
      geom_point(data = distance[distance$age_sup_15==1,],
               aes(Secteur,ecart_type_eco,color="âge < 5 ans")) +
    labs(colour = "Écart-type") +
    scale_color_manual(values = c("red","darkgreen","orange"),
                       labels = c("âge < 5 ans", "5 ans < âge < 15 ans", "âge > 15 ans")) +
    theme_minimal() +
    ylab("Valeur") +
    theme(axis.text.x = element_text(angle = 90))
  ggplotly(p)%>% #Ploty du graph
  config(displayModeBar = FALSE)
```




# Repenser l'agriculture : Clusters agricoles

## Column {data-width="350"}

### Nombre optimal de clusters WSS
```{r preparation cluster 1, echo=FALSE, message = FALSE, warning = FALSE}
df_cluster <- df_tot[,c("NOM_ASSUJ","COD_ACT_ECON_CAE","COD_ACT_ECON_CAE2","Long","Lat")]
df_cluster <- df_cluster[!duplicated(df_cluster$NOM_ASSUJ),]
rownames(df_cluster) <-df_cluster$NOM_ASSUJ
fviz_nbclust(df_cluster[,c(4,5)], kmeans,method ="wss")

```

```{r preparation cluster 2, echo=FALSE, message = FALSE, warning = FALSE,include=FALSE}
fviz_nbclust(df_cluster[,c(4,5)], kmeans,method ="silhouette")
```


### Visualisation de la composition des clusters
```{r preparation cluster 3, echo=FALSE, message = FALSE, warning = FALSE,include=FALSE}
set.seed(123)
cluster1 <- kmeans(df_cluster[,c(4,5)],4, iter.max = 10, nstart = 2)
df_cluster <- cbind(df_cluster, cluster1$cluster)
colnames(df_cluster)[6] <-"cluster"
```

```{r preparation cluster 4, echo=FALSE, message = FALSE, warning = FALSE,include=FALSE}
distance_cluster <- as.data.frame(cluster1$centers)
distance_cluster[3] <- cluster1$size
distance_cluster[4] <- round(cluster1$withinss,4)
distance_cluster$Var1 <- c(1:4)
distance_cluster$Var1 <- as.factor(distance_cluster$Var1)
colnames(distance_cluster)[3] <- "size"  
colnames(distance_cluster)[4] <- "withinss"  

index <- df_cluster %>%
  filter(COD_ACT_ECON_CAE == 131 | COD_ACT_ECON_CAE2 == 131) 
distance_cluster$v131 <- table(index$cluster)


index <- df_cluster %>%
  filter(COD_ACT_ECON_CAE == 134 | COD_ACT_ECON_CAE2 == 134) 
distance_cluster <- left_join(distance_cluster,as.data.frame(table(index$cluster)),by="Var1")
colnames(distance_cluster)[7] <- "v134"
distance_cluster$v134[3:4] <-0

index <- df_cluster %>%
  filter(COD_ACT_ECON_CAE == 135 | COD_ACT_ECON_CAE2 == 135) 
distance_cluster$v135 <- 0
distance_cluster$v135 <- table(index$cluster)

index <- df_cluster %>%
  filter(COD_ACT_ECON_CAE == 138 | COD_ACT_ECON_CAE2 == 138) 
distance_cluster$v138 <- 0
distance_cluster$v138 <- table(index$cluster)

index <- df_cluster %>%
  filter(COD_ACT_ECON_CAE == 139 | COD_ACT_ECON_CAE2 == 139) 
distance_cluster$v139 <- 0
distance_cluster$v139 <- table(index$cluster)

index <- df_cluster %>%
  filter(COD_ACT_ECON_CAE == 151 | COD_ACT_ECON_CAE2 == 151) 
distance_cluster$v151 <- 0
distance_cluster$v151 <- table(index$cluster)

index <- df_cluster %>%
  filter(COD_ACT_ECON_CAE == 152 | COD_ACT_ECON_CAE2 == 152)
distance_cluster$v152 <- 0
distance_cluster$v152 <- table(index$cluster)

index <- df_cluster %>%
  filter(COD_ACT_ECON_CAE == 159 | COD_ACT_ECON_CAE2 == 159) 
distance_cluster$v159 <- 0
distance_cluster$v159 <- table(index$cluster)

index <- df_cluster %>%
  filter(COD_ACT_ECON_CAE == 161 | COD_ACT_ECON_CAE2 == 161) 
distance_cluster$v161 <- 0
distance_cluster$v161 <- table(index$cluster)

index <- df_cluster %>%
  filter(COD_ACT_ECON_CAE == 162 | COD_ACT_ECON_CAE2 == 162)
distance_cluster$v162 <- 0
distance_cluster$v162 <- table(index$cluster)

index <- df_cluster %>%
  filter(COD_ACT_ECON_CAE == 163 | COD_ACT_ECON_CAE2 == 163) 
distance_cluster$v163 <- 0
distance_cluster$v163 <- table(index$cluster)

index <- df_cluster %>%
  filter(COD_ACT_ECON_CAE == 164 | COD_ACT_ECON_CAE2 == 164) 
distance_cluster$v164 <- 0
distance_cluster$v164 <- table(index$cluster)

index <- df_cluster %>%
  filter(COD_ACT_ECON_CAE == 169 | COD_ACT_ECON_CAE2 == 169) 
distance_cluster$v169 <- 0
distance_cluster$v169 <- table(index$cluster)

index <- df_cluster %>%
  filter(COD_ACT_ECON_CAE == 171 | COD_ACT_ECON_CAE2 == 171) 
distance_cluster$v171 <- 0
distance_cluster$v171 <- table(index$cluster)

index <- as.data.frame(t(distance_cluster)) 
index <- index[-c(1:5),]
index$V1 <- as.numeric(index$V1)
index$V2 <- as.numeric(index$V2)
index$V3 <- as.numeric(index$V3)
index$V4 <- as.numeric(index$V4)
distance_cluster$sum <- colSums(index)
distance_cluster <- mutate(distance_cluster, withinss = round(withinss/size,4))


for (i in 6:19) {
  distance_cluster[,i] <- round(distance_cluster[,i] / distance_cluster$sum,4)
}

long_cluster <- gather(distance_cluster, key="Secteur", value = "pourcentage", -Long,-Lat,-size,-withinss, -Var1,-sum)

long_cluster <- long_cluster %>%
  mutate(Secteur = case_when(Secteur == "v131" ~ "Céréales",Secteur == "v134" ~ "Maïs", Secteur == "v135" ~ "Plantes fourragères",
                             Secteur == "v138" ~ "Pommes de terre", Secteur == "v139" ~ "Autres grandes cultures", Secteur == "v151" ~ "Fruits",
                             Secteur == "v152" ~ "Légumes", Secteur == "v159" ~ "Mixte fruits et légumes", Secteur == "v161" ~ "Champignons",
                             Secteur == "v162" ~ "Serres", Secteur == "v163" ~ "Pépinières", Secteur == "v164" ~ "Érablières",
                             Secteur == "v169" ~ "Autres cultures horticoles", Secteur == "v171" ~ "Élev., grandes cult. et prod. hort.",
                             TRUE ~Secteur))

```


```{r, echo=FALSE, message = FALSE, warning = FALSE}
##
p <- ggplot(long_cluster) +
  geom_bar(aes(x=Var1,y=pourcentage,fill =Secteur),stat="identity") +
  coord_flip() +
  xlab("Numero de cluster") +
  theme_minimal() +
  scale_fill_manual(values = mycolors2)

ggplotly(p)%>% #Ploty du graph
  config(displayModeBar = FALSE)
```

## Column {data-width="650"}

```{r, echo=FALSE, message = FALSE, warning = FALSE, include = FALSE}
getColor <- function(df_cluster) {
  sapply(df_cluster$cluster, function(cluster) {
  if(cluster == 1) {
'#d7191c'
    } else if(cluster == 2) {
'#fdae61'
  } else if(cluster == 3) {
"#1b7837"
  } else if(cluster == 4) { 
'#2b83ba'
  } })
}

df_cluster <- arrange(df_cluster,cluster)
df_cluster[7] <- getColor(df_cluster)
colnames(df_cluster)[7] <- "couleur"

index <- df_cluster %>%
  filter(cluster == 1)
distance_cluster$withinss[1] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance_cluster$ecart_type[1] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)

index <- df_cluster %>%
  filter(cluster == 2)
distance_cluster$withinss[2] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance_cluster$ecart_type[2] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)

index <- df_cluster %>%
  filter(cluster == 3)
distance_cluster$withinss[3] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance_cluster$ecart_type[3] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)

index <- df_cluster %>%
  filter(cluster == 4)
distance_cluster$withinss[4] <- round(mean(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2),na.rm=TRUE),4)
distance_cluster$ecart_type[4] <- round(sd(sqrt((index$Lat - mean(index$Lat,na.rm = TRUE))^2 + (index$Long - mean(index$Long,na.rm = TRUE))^2), na.rm = TRUE),4)

df_cluster <- df_cluster %>%
  mutate(cluster = case_when(cluster == 1 ~ "Cluster 1",cluster == 2 ~ "Cluster 2",
                              cluster == 3 ~ "Cluster 3",cluster == 4 ~ "Cluster 4"))
```

### Visualisation des entreprises
```{r visualisation cluster 1, echo=FALSE, message = FALSE, warning = FALSE}

leaflet(data = df_cluster) %>% 
  addTiles() %>% 
  addCircleMarkers(lng = ~Long, lat = ~Lat, color = df_cluster$couleur, popup = ~rownames(df_cluster)) %>%
  addLegend(
    "bottomright",
    title = "Appartenance des entreprises",
    colors = unique(df_cluster$couleur),
    labels = unique(df_cluster$cluster),
    opacity = 0.8) 

```



### Visualisation des clusters
```{r, echo=FALSE, message = FALSE, warning = FALSE}

#table(df_cluster$COD_ACT_ECON_CAE2,df_cluster$cluster)
col_cluster <-c('#fcbba1','#fc9272','#fb6a4a','#de2d26','#a50f15')


leaflet(data=distance_cluster) %>%
  addTiles() %>%
  addCircleMarkers(lng = ~Long, lat = ~Lat,radius = ~15*withinss,
                   color =~colorBin(col_cluster,size)(size), stroke = TRUE, fillOpacity = 0.7,
                   popup = ~paste("Distance moyenne:",withinss, "<br/>","taille du cluster:", size)) %>%
  addCircleMarkers(lng = ecosysteme$Longitude[1], lat = ecosysteme$Latitude[1], ## On ajoute le centroïde de l'écosystème
      radius = 15, stroke = FALSE, fillOpacity = 0.9, 
      color = "steelblue", popup = ecosysteme$Nom_ent[1]) %>%
  addLegend(
    "bottomleft",
    title = "Taille du cluster",
    pal = colorBin(col_cluster, distance_cluster$size,5),
    values = distance_cluster$size, 
    opacity = 0.9)

```